package com.senior_design.traffic_map;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
 
public class TrafficMapActivity extends Activity {
	private MapView myMapView;
	private MapController myMapController;
	//private String tileSourceName = "mapnik";
	LocationManager locationManager;
	
	ArrayList<OverlayItem> overlayItemArray;
	
	//MyItemizedOverlay myItemizedOverlay = null;
	MyLocationOverlay myLocationOverlay = null;

   
	@Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myMapView = (MapView)findViewById(R.id.openmapview);
        myMapView.setTileSource(TileSourceFactory.MAPNIK);
        myMapView.setClickable(true);
        myMapView.setBuiltInZoomControls(true);
        
                
        myMapController = myMapView.getController();
        myMapController.setZoom(12);
        
        overlayItemArray = new ArrayList<OverlayItem>();
        
        DefaultResourceProxyImpl defaultResourceProxyImpl = new DefaultResourceProxyImpl(this);
        MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(overlayItemArray, null, defaultResourceProxyImpl);
        myMapView.getOverlays().add(myItemizedOverlay);
        
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(lastLocation != null){
         updateLoc(lastLocation);
        }
        
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
        myMapView.getOverlays().add(myScaleBarOverlay);
        
    }
    
    @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  locationManager.removeUpdates(myLocationListener);
 }

 private void updateLoc(Location loc){
     GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
     myMapController.setCenter(locGeoPoint);
     setOverlayLoc(loc);
     myMapView.invalidate();
    }
 
 private void setOverlayLoc(Location overlayloc){
	  GeoPoint overlocGeoPoint = new GeoPoint(overlayloc);
	  //---
	     overlayItemArray.clear();
	     
	     OverlayItem newMyLocationItem = new OverlayItem(
	       "My Location", "My Location", overlocGeoPoint);
	     overlayItemArray.add(newMyLocationItem);
	     //---
	 }
    
    private LocationListener myLocationListener
    = new LocationListener(){

  @Override
  public void onLocationChanged(Location location) {
   // TODO Auto-generated method stub
   updateLoc(location);
  }

  @Override
  public void onProviderDisabled(String provider) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onProviderEnabled(String provider) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
   // TODO Auto-generated method stub
   
  }
     
    };

    private class MyItemizedOverlay extends ItemizedIconOverlay<OverlayItem>{

    	  public MyItemizedOverlay(List<OverlayItem> pList,org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,ResourceProxy pResourceProxy) {
    	   super(pList, pOnItemGestureListener, pResourceProxy);
    	   // TODO Auto-generated constructor stub
    	  }

    	  @Override
    	  public void draw(Canvas canvas, MapView mapview, boolean arg2) {
    	   // TODO Auto-generated method stub
    	   super.draw(canvas, mapview, arg2);
    	   
    	   if(!overlayItemArray.isEmpty()){
    	    
    	    //overlayItemArray have only ONE element only, so I hard code to get(0)
    	    GeoPoint in = overlayItemArray.get(0).getPoint();
    	    
    	    Point out = new Point();
    	    mapview.getProjection().toPixels(in, out);
    	    
    	    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_mylocation);
    	    canvas.drawBitmap(bm,out.x - bm.getWidth()/2,  //shift the bitmap center
    	    		out.y - bm.getHeight()/2,  //shift the bitmap center
    	      null);
    	   }
    	  }

    	  @Override
    	  public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
    	   // TODO Auto-generated method stub
    	   //return super.onSingleTapUp(event, mapView);
    	   return true;
    	  }
    	    }
    	}
         
        